﻿using System;
using System.IO;
using PlugNT.Common;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_FileManager
{
    public partial class _Edit : AdminPage
    {
        public string root = "";
        public string file = "";
        private string editFileType = "aspx,html,htm,js,css,txt";
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack && Request["file"] != null && Request["root"] != null)
            {
                file = Request["file"].ToLower();
                root = Request["root"].ToLower();
                if (type())
                {
                    try
                    {
                        TextBox_HTML.Text = Utils.ReadFile(Server.MapPath(file));
                    }
                    catch
                    {
                        TextBox_HTML.Text = Utils.ReadFile(file);
                    }
                }
                else
                {
                    Response.Redirect(file);
                }
            }
        }

        public bool type()
        {
            int r = 0;
            string[] RootArray = editFileType.Split(',');
            for (int i = 0; i < RootArray.Length; i++)
            {
                r += file.LastIndexOf("." + RootArray[i]);
            }
            if (r > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected void imgBtnUpdate_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            Utils.CreateFile(Server.MapPath(Request["page"]), TextBox_HTML.Text);
            Response.Redirect("../Message.aspx?message=文件更新成功！&z=yes");
        }
    }
}